package com.xk.sms.utils;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import com.xk.sms.config.SmsProperties;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

/**
 * 发送消息
 */
@Component
@EnableConfigurationProperties(SmsProperties.class)
@Slf4j
public class SmsUtils{
    //产品名称:云通信短信API产品,开发者无需替换
    static final String product = "Dysmsapi";
    //产品域名,开发者无需替换
    static final String domain = "dysmsapi.aliyuncs.com";
    private static final String KEY_PREFIX="xk.phone";
    private static final long SMS_MIN_TIME=300000;//时长为五分钟

    @Autowired
    private SmsProperties prop;
    @Autowired
    private StringRedisTemplate redisTemplate;

    //方法中的参数有电话号码，短信签名，短信模板,模板json字符串
    public  SendSmsResponse sendSms(String phone,String signName, String templateCode,
                                    String templateParam) throws ClientException {
/*        String key=KEY_PREFIX+phone;
        //按照手机号码限流
        String lastTime = redisTemplate.opsForValue().get(key);
        if (StringUtils.isNoneBlank(lastTime)){//不为null
            Long last = Long.valueOf(lastTime);
            if (System.currentTimeMillis()-last<SMS_MIN_TIME){//小于3分钟就阻止发送消息
                log.info("验证码发送过于频繁 ");
            }
        }*/
        //可自助调整超时时间
        System.setProperty("sun.net.client.defaultConnectTimeout", "10000");
        System.setProperty("sun.net.client.defaultReadTimeout", "10000");

        //初始化acsClient,暂不支持region化
        IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", prop.getAccessKeyId(), prop.getAccessKeySecret());
        DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", product, domain);
        IAcsClient acsClient = new DefaultAcsClient(profile);

        //组装请求对象-具体描述见控制台-文档部分内容
        SendSmsRequest request = new SendSmsRequest();
        //必填:待发送手机号
        request.setPhoneNumbers(phone);
        //必填:短信签名-可在短信控制台中找到
        request.setSignName(signName);
        //必填:短信模板-可在短信控制台中找到
        request.setTemplateCode(templateCode);
        //可选:模板中的变量替换JSON串,如模板内容为"亲爱的${name},您的验证码为${code}"时,此处的值为
        request.setTemplateParam(templateParam);
        //hint 此处可能会抛出异常，注意catch
        SendSmsResponse sendSmsResponse = acsClient.getAcsResponse(request);

        if (!sendSmsResponse.getCode().equals("OK")){//没发送成功
            log.info("发送验证码失败："+sendSmsResponse.getMessage());
        }
        log.info("验证码发送成功："+sendSmsResponse.getMessage());
       /* *//*发送短信成功后写入到redis中,key就是电话号码，value是当前时间,
        并设置3分钟后自动删除*//*
        redisTemplate.opsForValue().set(key,String.valueOf(System.currentTimeMillis()),
                5, TimeUnit.MINUTES);*/
        return sendSmsResponse;

    }
}
